package yur.wedding.modules.sys.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import yur.wedding.common.annotation.SysLog;
import yur.wedding.common.utils.Constant;
import yur.wedding.exception.RRException;
import yur.wedding.modules.sys.entity.SysMenuEntity;
import yur.wedding.modules.sys.service.SysMenuService;


import yur.wedding.utils.R;



/**
 * 菜单管理
 *
 * @author Yur
 * @email ${email}
 * @date 2021-11-09 21:15:11
 */
@RestController
@RequestMapping("sys/sysmenu")
public class SysMenuController extends AbstractController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 导航栏菜单
     */
    @RequestMapping("/nav")
    public R nav() {

        List<SysMenuEntity> menulist = sysMenuService.getUserMenuList(getUserId());

        return R.ok().put("menulist", menulist);
    }


    /**
     * 列表  所有菜单
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysmenu:list")
    public List list(@RequestParam Map<String, Object> params) {


        List<SysMenuEntity> menulist = sysMenuService.list();

        for (SysMenuEntity sysmlist : menulist) {
            //获取父类名称
            SysMenuEntity sysMenuEntity = sysMenuService.getById(sysmlist.getParentId());

            if (sysMenuEntity != null) {

                sysmlist.setParentName(sysMenuEntity.getName());

            }

        }
        return menulist;
    }

    /**
     * 查询展示的 菜单 不包含按钮
     */

    @RequestMapping("/select")
    @RequiresPermissions("sys:sysmenu:select")
    public R select() {
        System.out.println("单向查询");
        List<SysMenuEntity> list = sysMenuService.queryNotButtonList();
        SysMenuEntity root = new SysMenuEntity();
        root.setMenuId(0L);
        root.setName("一级菜单");
        root.setParentId(-1L);
        root.setOpen(true);
        list.add(root);
        //设置顶级菜单

        System.out.println("menuList:" + list);
        return R.ok().put("menuList", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
    @RequiresPermissions("sys:sysmenu:info")
    public R info(@PathVariable("menuId") Long menuId) {
        SysMenuEntity sysMenu = sysMenuService.getById(menuId);

        return R.ok().put("menu", sysMenu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysmenu:save")
    public R save(@RequestBody SysMenuEntity sysMenu) {
        //校验
        verifyForm(sysMenu);
        sysMenuService.save(sysMenu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysmenu:update")
    public R update(@RequestBody SysMenuEntity sysMenu) {
        //数据校验
        verifyForm(sysMenu);
        sysMenuService.updateById(sysMenu);

        return R.ok();
    }

    /**
     * 删除
     * SysLog 切面日志信息
     */
    @SysLog("删除菜单")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysmenu:delete")
    public R delete(Long menuIds) {

        if (menuIds <= 31 || menuIds == 34 ||menuIds==38) {
            return R.error("系统菜单不能删除");
        }

        List<SysMenuEntity> entities = sysMenuService.queryListParentId(menuIds);
        if (entities.size() > 0) {
            return R.error("存在子菜单无法删除");
        }
        sysMenuService.removeByIds(Arrays.asList(menuIds));

        return R.ok();
    }


    /**
     * 校验参数
     */
    public void verifyForm(SysMenuEntity sysMenu) {

        if (StringUtils.isEmpty(sysMenu.getName())) {
            throw new RRException("菜单名称不能为空");
        }

        if (sysMenu.getParentId() == null) {
            throw new RRException("上级菜单不能为空");
        }
        //菜单
        if (sysMenu.getType() == Constant.MenuType.MENU.getValue()) {
            if (sysMenu.getUrl() == null) {
                throw new RRException("菜单地址不能为空");
            }
        }
        //获取上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (sysMenu.getParentId() != 0) {
            SysMenuEntity byId = sysMenuService.getById(sysMenu.getParentId());
            parentType = byId.getType();

        }


        //目录、菜单
        if (sysMenu.getType() == Constant.MenuType.MENU.getValue() || sysMenu.getType() == Constant.MenuType.CATALOG.getValue()) {
            //目录 菜单 的上级都只能是 目录类型
            if (parentType != Constant.MenuType.CATALOG.getValue()) {
                throw new RRException("上级菜单只能为目录类型");
            }

            return;

        }

        //按钮
        if (sysMenu.getType() == Constant.MenuType.BUTTON.getValue()) {
            if (parentType != Constant.MenuType.MENU.getValue()) {
                throw new RRException("上级菜单只能为菜单类型");
            }
            return;
        }

    }
}
